class SerialUtils:
    alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZ"

    @staticmethod
    def encode_serial_number(origin_id: int, table_number: int) -> str:
        """
        Generates a serial number from the original snowflake ID and table number.
        """

        return ""

    @staticmethod
    def decode_serial_number(code: str):
        """
        Decodes the serial number into its original snowflake ID and table number.
        """

        pass

    @staticmethod
    def encode_table_index(table_index: int):
        """
        Encodes the table index into a alphabet string.
        """
        if table_index < 0 or table_index > 127:
            raise ValueError("Table index must be between 0 and 127.")
        if table_index == 0:
            return SerialUtils.alphabet[0] * 2  # 2位
        result = ""
        while table_index > 0:
            table_index, remainder = divmod(table_index, len(SerialUtils.alphabet))
            result = str(SerialUtils.alphabet[remainder]) + result
        if len(result) < 2:
            result = SerialUtils.alphabet[0] * (2 - len(result)) + result
        return result

    @staticmethod
    def decode_table_index(code: str):
        """
        Decodes the alphabet string into the table index.
        """
        alphabet = SerialUtils.alphabet
        if len(code) != 2:
            raise ValueError("Encoded string must be two characters long.")
        value = 0
        for char in code:
            if char not in alphabet:
                raise ValueError(f"Invalid character '{char}' in encoded string.")
            value *= len(alphabet)
            value += alphabet.index(char)
        return value

    @staticmethod
    def encode_serial(snowflake_id: int):
        """
        Encodes the snowflake_id into a alphabet string.
        """
        result = ""
        while snowflake_id > 0:
            snowflake_id, remainder = divmod(snowflake_id, len(SerialUtils.alphabet))
            result = str(SerialUtils.alphabet[remainder]) + result
        if len(result) < 3:
            result = SerialUtils.alphabet[0] * (3 - len(result)) + result
        return result

    @staticmethod
    def decode_serial(code: str):
        """
        Decodes the alphabet string into the table index.
        """
        alphabet = SerialUtils.alphabet
        # if len(code) != 2:
        #     raise ValueError("Encoded string must be two characters long.")
        value = 0
        for char in code:
            if char not in alphabet:
                raise ValueError(f"Invalid character '{char}' in encoded string.")
            value *= len(alphabet)
            value += alphabet.index(char)
        return value

